import { defineStore } from 'pinia'
import type { Category } from "./interface";
import { getTowClassificationAttr, getThreeClassificationAttr, getOneClassificationAttr } from '@/Api/product/attr'

/**
 * 1.当前模块要管理哪些数据
 *   看那些数据多个组件使用
 * 2.数据的初始值为什么?
 * 
 */
export const useCategoryStore = defineStore("category", {
  state: (): Category => ({
    // 初始化值为undefined,能让select 组件一开始补选中中任何内容
    // 如果是0的话,select组件一开始会渲染一个0
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
    Addshow: true
  }),
  // state数据在页面不能直接展示,要做一些处理/计算才能展示,才需要getters
  getters: {},
  // 数据展示要不要发送请求得到/操作
  actions: {
    // 获取一级分类列表
    async getCategory1List() {
      this.category1List = await getOneClassificationAttr();
    },
    // 获取二级分类列表
    async getCategory2List(category1Id: number) {
      this.category2List = await getTowClassificationAttr(category1Id);
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
    },
    // 获取三级级分类列表
    async getCategory3List(category2Id: number) {
      this.category3List = await getThreeClassificationAttr(category2Id);
      this.category3Id = undefined
    }
  }
})
